package com.zl.iot.docking.exception;


import com.zl.iot.docking.consts.DEFAULT;
import org.apache.commons.lang3.StringUtils;

/**
 * @author chenzy
 * @since 2021-01-15
 */
public class MyExceptionNew extends RuntimeException {
    private Integer code;
    private Object data;

    private MyExceptionNew() {
        this(DEFAULT.CODE);
    }

    private MyExceptionNew(Integer code) {
        this(code, DEFAULT.EXCEPTION_MSG);
    }

    private MyExceptionNew(String message) {
        this(DEFAULT.CODE, message);
    }

    private MyExceptionNew(Integer code, String message) {
        this(code, message, null);
    }

    private MyExceptionNew(Integer code, String message, boolean fillInStackTrace) {
        super(fillInStackTrace ? null : message, fillInStackTrace ? new RuntimeException(message) : null);
        this.code = code;
    }

    private MyExceptionNew(Integer code, String message, Throwable e) {
        super(message, e);
        this.code = code;
    }

    public static <T> MyExceptionNew error(Integer code, String msg, T data) {
        return new MyExceptionNew(code, msg).setData(data);
    }

    public static MyExceptionNew error(Integer code, String message) {
        return new MyExceptionNew(code, message);
    }

    public static MyExceptionNew error(Integer code, String message, boolean fillInStackTrace) {
        return new MyExceptionNew(code, message, fillInStackTrace);
    }


    public static MyExceptionNew error(Integer code, String message, Throwable throwable) {
        return new MyExceptionNew(code, message, throwable);
    }
    public Object getData() {
        return data;
    }
    public MyExceptionNew setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public String getMessage() {
        var msg = super.getMessage();
        if (StringUtils.isBlank(msg)) {
            return DEFAULT.EXCEPTION_MSG;
        }
        return msg;
    }
    /**
     * 重写堆栈填充，大多数情况不需要填充错误堆栈信息，提高性能
     */
    @Override
    public MyExceptionNew fillInStackTrace() {
        return this;
    }
    public Integer getCode() {
        return code;
    }
}

